/*!
 * \file redis_core.c
 *
 * \brief 共通定义
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 * 共通使用的宏和结构体
 *
 * \version 1.0
 * \author jikw
 * \date 2015/11/16
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "frreport_comm.h"
#include "frct_comm.h"
#include "frservice_comm.h"
#include "frbyte_buffer.h"
#include "redis_core.h"

extern void *g_p_apm_context;

typedef struct __st_L7session_redis_private
{
    unsigned long ul_seq;                             //序列号，1次req和response时，使用同一个序列号
    L4_transaction st_l4_trans;                       //数据
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    Frbyte_Packet *p_resp_packet;                     //存相应bytebuff数据
}L7session_redis_private;

/*!
 * redis私有数据创建
 *
 * \param void
 *
 * \return NULL          内存分配失败\n
 *         p_st_private  内存分配成功
 *
 * \note
 *
 */
FR_STATIC L7session_redis_private* __redis_private_create(void)
{
    L7session_redis_private *p_st_private = NULL;

    p_st_private = (L7session_redis_private*)frmalloc(sizeof(L7session_redis_private));
    if (p_st_private == NULL)
        return NULL;

    memset(p_st_private, 0x00, sizeof(L7session_redis_private));
    return p_st_private;
}

/*!
 * 私有信息重置
 *
 * \param p_private_session  (redis的私有session数据)
 *
 * \return FR_SUCC   成功\n
 *
 * \note
 *
 */
FR_STATIC int __redis_session_reset(void *p_private_session)
{
    L7session_redis_private *p_st_private = (L7session_redis_private*)p_private_session;

    if (p_st_private)
    {
        if (p_st_private->p_st_body_data_req)
            frreport_body_free(&p_st_private->p_st_body_data_req);

        if (p_st_private->p_st_body_data_resp)
            frreport_body_free(&p_st_private->p_st_body_data_resp);

        if (p_st_private->p_lst_req)
            frreport_body_l7list_free(&p_st_private->p_lst_req);

        if (p_st_private->p_lst_comm)
            frreport_body_l7list_free(&p_st_private->p_lst_comm);

        if (p_st_private->p_resp_packet)
            bytebuff_free(&(p_st_private->p_resp_packet));
    }
    return FR_SUCC;
}

/*!
 * 判断是否是redis协议
 *
 * \param p_st_conn      (输入数据信息)
 * \param p_st_packet    (输入数据)
 *
 * \return APM_L7_IS_YES  属于redis协议\n
 *         APM_L7_IS_NO   不属于redis协议
 *
 * \note
 *
 */
int redis_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet)
{
    int i_ret = 0;
    int i_ret_fun = 0;
    char *p_pos = NULL;

    if (p_st_packet == NULL || p_st_packet->p_payload == NULL || p_st_packet->ui_payload_len == 0 || p_st_conn==NULL)
        return APM_L7_IS_NO;
    //redis协议request格式：
    //ASTERISK args.length+1 DOLLAR command.length AUTH     CRLF DOLLAR arg.length password         CRLF
    //2a       320d0a        24     340d0a         41555448 0d0a 24     380d0a     666f6f6261726564 0d0a
    if (*p_st_packet->p_payload != '*')
        goto error;
    if (*(p_st_packet->p_payload+1) < '1' || *(p_st_packet->p_payload+1) > '9')
        goto error;
    p_pos = frmemstr(p_st_packet->p_payload, p_st_packet->ui_payload_len, (char*)"\r\n", 2);
    if (p_pos == NULL)
        goto error;
    if (*(p_pos+2) != '$')
        goto error;

    i_ret_fun = frct_get_direct_known(p_st_conn);
    if ( i_ret_fun == FR_DIRECT_UNKNOWN)
        frcap_set_cs_srv(p_st_conn, p_st_packet->dst_ip, p_st_packet->dst_port);

    frservice_add(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0);
    return APM_L7_IS_YES;

error:
    if (frct_get_direct_known(p_st_conn) == FR_DIRECT_UNKNOWN)
    {
        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
        {
            frcap_set_cs_srv(p_st_conn, p_st_packet->dst_ip, p_st_packet->dst_port);
            return APM_L7_IS_YES;
        }
        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
        {
            frcap_set_cs_srv(p_st_conn, p_st_packet->src_ip, p_st_packet->src_port);
            return APM_L7_IS_YES;
        }
    }
    else
    {
        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
            return APM_L7_IS_YES;

        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
            return APM_L7_IS_YES;
    }
    return APM_L7_IS_NO;
}

/*!
 * 判断字符串中每个字符是否全为数字
 *
 * \param p_data
 * \param i_len
 *
 * \return 1   全为数字\n
 *         0   不全为数字
 *
 * \note
 *
 */
FR_STATIC int __is_digit(char *p_data, int i_len)
{
    int i_tmp = 0;

    if (*p_data == '0')
    {
        if (i_len == 1)
            return 1;
        else
            return 0;
    }

    if (*p_data<'1' || *p_data>'9')
        return 0;

    for (i_tmp=1;i_tmp<i_len;i_tmp++)
    {
        if (*(p_data+i_tmp)<'0' || *(p_data+i_tmp)>'9')
            return 0;
    }
    return 1;
}

/*!
 * req数据解析
 *
 * \param p_data          (req数据)
 * \param ui_data_len     (数据长度)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 * \param p_st_conn       (入力链接信息)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __req_parse(char *p_data, unsigned int ui_data_len, L7session_redis_private *p_st_private, Frreport_body_data *p_st_body_data, Conntrack *p_st_conn)
{
    unsigned int ui_len_tmp = 0;
    char *p_pos = NULL, *p_pos2 = NULL;
    char *p_name = NULL;
    char *p_value = NULL;
    int i_count = 0;
    char arr_name[32];
    char arr_len[8];

    if (p_data == NULL || ui_data_len == 0 || p_st_private == NULL || p_st_body_data == NULL || p_st_conn == NULL)
        return APM_L7_ANALYZE_FAIL;

    memset(arr_name, 0x00, sizeof(arr_name));
    memset(arr_len, 0x00, sizeof(arr_len));

    p_pos = frmemstr(p_data, ui_data_len, (char*)"\r\n$", 3);
    if (p_pos == NULL)
        goto ERROR;
    p_pos2 = frmemstr(p_pos+1, ui_data_len-(p_pos-p_data)-1, (char*)"\r\n", 2);
    if (p_pos2 == NULL)
        goto ERROR;
    ui_len_tmp = p_pos2-p_pos-3+1;
    snprintf(arr_len, ui_len_tmp, "%s", p_pos+3);
    if (__is_digit(arr_len,ui_len_tmp-1)==0)
        goto ERROR;
    ui_len_tmp = atoi(arr_len)+1;

    p_name = (char*)frmalloc(ui_len_tmp);
    if (p_name == NULL)
        goto ERROR;
    snprintf(p_name, ui_len_tmp, "%s", p_pos2+2);

    if (memcmp(p_pos2+2, "CLIENT", 6) != 0 && memcmp(p_pos2+2, "CONFIG", 6) != 0 && memcmp(p_pos2+2, "SCRIPT", 6) != 0)
    {
        if (frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, p_name) == FR_FAIL)
            goto ERROR;
        if (frreport_body_l7_add_str(p_st_body_data, "request.redis.cmd", p_name) == FR_FAIL)
            goto ERROR;
        frfree(p_name);
        p_name = NULL;
    }
    else
    {
        p_pos = frmemstr(p_pos2+1, ui_data_len-(p_pos2-p_data)-1, (char*)"\r\n$", 3);
        if (p_pos == NULL)
            goto ERROR;
        p_pos2 = frmemstr(p_pos+1, ui_data_len-(p_pos-p_data)-1, (char*)"\r\n", 2);
        if (p_pos2 == NULL)
            goto ERROR;
        ui_len_tmp = p_pos2-p_pos-3+1;
        snprintf(arr_len, ui_len_tmp, "%s", p_pos+3);
        if (__is_digit(arr_len,ui_len_tmp-1)==0)
            goto ERROR;
        ui_len_tmp = atoi(arr_len)+1;
        p_value = (char*)frmalloc(ui_len_tmp);
        if (p_value == NULL)
            goto ERROR;
        snprintf(p_value, ui_len_tmp, "%s", p_pos2+2);
        if (frstrupper(p_value, ui_len_tmp) == FR_FAIL)
            goto ERROR;
        snprintf(arr_name, sizeof(arr_name), "%s %s", p_name, p_value);
        if (frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, arr_name) == FR_FAIL)
            goto ERROR;
        if (frreport_body_l7_add_str(p_st_body_data, "request.redis.cmd", arr_name) == FR_FAIL)
            goto ERROR;
        frfree(p_name);
        p_name = NULL;
        frfree(p_value);
        p_value = NULL;
    }

    for (;;)
    {
        p_pos = frmemstr(p_pos2+1, ui_data_len-(p_pos2-p_data)-1, (char*)"\r\n$", 3);
        if (p_pos == NULL)
        {
            if (frreport_body_l7_add_int(p_st_body_data, "request.redis.param.size", i_count) == FR_FAIL)
                goto ERROR;
            return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);
        }
        p_pos2 = frmemstr(p_pos+1, ui_data_len-(p_pos-p_data)-1, (char*)"\r\n", 2);
        if (p_pos2 == NULL)
            goto ERROR;
        ui_len_tmp = p_pos2-p_pos-3+1;
        snprintf(arr_len, ui_len_tmp, "%s", p_pos+3);
        if (__is_digit(arr_len,ui_len_tmp-1)==0)
            goto ERROR;
        ui_len_tmp = atoi(arr_len)+1;
        p_value = (char*)frmalloc(ui_len_tmp);
        if (p_value == NULL)
            goto ERROR;
        snprintf(p_value, ui_len_tmp, "%s", p_pos2+2);
        snprintf(arr_name, sizeof(arr_name), "request.redis.param.%d", ++i_count);
        //公用函数，判断字符串p_value中是否含有不可见字符，如果有，返回0
        if (fris_print_ascii(p_value, ui_len_tmp-1)==0)
        {
            if (frreport_body_l7_add_bin(p_st_body_data, arr_name, p_value, ui_len_tmp-1) == FR_FAIL)
                goto ERROR;
        }
        else
        {
            if (frreport_body_l7_add_str(p_st_body_data, arr_name, p_value) == FR_FAIL)
                goto ERROR;
        }
        frfree(p_value);
        p_value = NULL;
    }
    return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);

ERROR:
    if (p_name)
        frfree(p_name);
    if (p_value)
        frfree(p_value);

    return APM_L7_ANALYZE_FAIL;
}

/*!
 * resp数据解析
 *
 * \param p_packet        (resp数据)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 * \param p_st_conn       (入力链接信息)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __resp_parse(Frbyte_Packet *p_packet, L7session_redis_private *p_st_private, Frreport_body_data *p_st_body_data, Conntrack *p_st_conn)
{
    int i_index = 0;
    int i_index_len = 0;
    int i_tmp = 0;
    int i_count = 0;
    unsigned int ui_len_tmp = 0;
    char *p_value = NULL;
    char *p_len = NULL;
    char arr_name[32];
    char arr_tmp[2];

    if (p_packet == NULL || p_st_private == NULL || p_st_body_data == NULL || p_st_conn == NULL)
        return APM_L7_ANALYZE_FAIL;

    memset(arr_name, 0x00, sizeof(arr_name));
    memset(arr_tmp, 0x00, sizeof(arr_tmp));

    //第一位存在arr_tmp判断是$开头还是*开头
    if (bytebuff_peek_bytes(p_packet, arr_tmp, 1) == FR_FAIL)
        goto ERROR;

    if (*arr_tmp == '$')
    {
        i_tmp = bytebuff_getPosition(p_packet);
        if (i_tmp == -1)
            goto ERROR;
        //如“$-1\r\n”
        if (bytebuff_memstr(p_packet, i_tmp, 0, "\r\n", 2, &i_index) == FR_FAIL)
            goto ERROR;

        if (i_index > bytebuff_remaining(p_packet))
            return APM_L7_ANALYZE_TODO;

        //略过'$'
        if (bytebuff_skip(p_packet, 1) != FR_SUCC)
            goto ERROR;
        p_len = (char*)frmalloc(i_index-1+1);
        if (p_len == NULL)
            goto ERROR;
        memset(p_len, 0x00, i_index-1+1);
        if (bytebuff_get_bytes(p_packet, p_len, i_index-1) == FR_FAIL)
            goto ERROR;

        if (strcmp(p_len,"-1")==0)
        {
            frfree(p_len);
            p_len = NULL;
            snprintf(arr_name, sizeof(arr_name), "response.redis.param.%d", ++i_count);
            if (frreport_body_l7_add_str(p_st_body_data, arr_name, "") == FR_FAIL)
                goto ERROR;
            if (frreport_body_l7_add_int(p_st_body_data, "response.redis.param.size", i_count) == FR_FAIL)
                goto ERROR;
            if (frreport_body_l7_add_int(p_st_body_data, APM_REPORT_KEY_RESP_CODE, 0) == FR_FAIL)
                goto ERROR;
            return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);
        }
        if (__is_digit(p_len,i_index-1)==0)
            goto ERROR;
        ui_len_tmp = atoi(p_len)+1;
        frfree(p_len);
        p_len = NULL;

        if (ui_len_tmp-1+2 > (unsigned int)bytebuff_remaining(p_packet))
            return APM_L7_ANALYZE_TODO;

        p_value = (char*)frmalloc(ui_len_tmp);
        if (p_value == NULL)
            goto ERROR;
        memset(p_value, 0x00, ui_len_tmp);
        //略过'\r\n'
        if (bytebuff_skip(p_packet, 2) != FR_SUCC)
            goto ERROR;
        if (bytebuff_get_bytes(p_packet, p_value, ui_len_tmp-1) == FR_FAIL)
            goto ERROR;
        snprintf(arr_name, sizeof(arr_name), "response.redis.param.%d", ++i_count);
        if (fris_print_ascii(p_value, ui_len_tmp-1)==0)
        {
            if (frreport_body_l7_add_bin(p_st_body_data, arr_name, p_value, ui_len_tmp-1) == FR_FAIL)
                goto ERROR;
        }
        else
        {
            if (frreport_body_l7_add_str(p_st_body_data, arr_name, p_value) == FR_FAIL)
                goto ERROR;
        }
        frfree(p_value);
        p_value = NULL;
        if (frreport_body_l7_add_int(p_st_body_data, "response.redis.param.size", i_count) == FR_FAIL)
            goto ERROR;
        if (frreport_body_l7_add_int(p_st_body_data, APM_REPORT_KEY_RESP_CODE, 0) == FR_FAIL)
            goto ERROR;
        return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);
    }

    if (*arr_tmp == '*')
    {
        for (;;)
        {
            i_tmp = bytebuff_getPosition(p_packet);
            if (i_tmp == -1)
                goto ERROR;
            if (bytebuff_memstr(p_packet, i_tmp, 0, "\r\n$", 3, &i_index) == FR_FAIL)
            {
                if (i_count==0)
                    goto ERROR;
                if (frreport_body_l7_add_int(p_st_body_data, "response.redis.param.size", i_count) == FR_FAIL)
                    goto ERROR;
                if (frreport_body_l7_add_int(p_st_body_data, APM_REPORT_KEY_RESP_CODE, 0) == FR_FAIL)
                    goto ERROR;
                return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);
            }
            if (i_index-i_tmp+3 > bytebuff_remaining(p_packet))
                return APM_L7_ANALYZE_TODO;
            if (bytebuff_skip(p_packet, i_index-i_tmp+3) != FR_SUCC)
                goto ERROR;
            i_tmp = bytebuff_getPosition(p_packet);
            if (i_tmp == -1)
                goto ERROR;
            if (bytebuff_memstr(p_packet, i_tmp, 0, "\r\n", 2, &i_index_len) == FR_FAIL)
                return APM_L7_ANALYZE_TODO;

            ui_len_tmp = i_index_len-i_tmp;
            if (ui_len_tmp > (unsigned int)bytebuff_remaining(p_packet))
                return APM_L7_ANALYZE_TODO;

            p_len = (char*)frmalloc(ui_len_tmp+1);
            if (p_len == NULL)
                goto ERROR;
            memset(p_len, 0x00, ui_len_tmp+1);
            if (bytebuff_get_bytes(p_packet, p_len, ui_len_tmp) == FR_FAIL)
                goto ERROR;
            if (strcmp(p_len,"-1")==0)
            {
                snprintf(arr_name, sizeof(arr_name), "response.redis.param.%d", ++i_count);
                if (frreport_body_l7_add_str(p_st_body_data, arr_name, "") == FR_FAIL)
                    goto ERROR;
                frfree(p_len);
                p_len = NULL;
                continue;
            }

            if (__is_digit(p_len,ui_len_tmp)==0)
                goto ERROR;
            if (atoi(p_len) != 0)
            {
                if (atoi(p_len)+2 > bytebuff_remaining(p_packet))
                    return APM_L7_ANALYZE_TODO;
                ui_len_tmp = atoi(p_len)+1;
                p_value = (char*)frmalloc(ui_len_tmp);
                if (p_value == NULL)
                    goto ERROR;
                memset(p_value, 0x00, ui_len_tmp);
                if (bytebuff_skip(p_packet, 2) != FR_SUCC)
                    goto ERROR;
                if (bytebuff_get_bytes(p_packet, p_value, atoi(p_len)) == FR_FAIL)
                    goto ERROR;
                snprintf(arr_name, sizeof(arr_name), "response.redis.param.%d", ++i_count);
                if (fris_print_ascii(p_value, ui_len_tmp-1) == 0)
                {
                    if (frreport_body_l7_add_bin(p_st_body_data, arr_name, p_value, ui_len_tmp-1) == FR_FAIL)
                        goto ERROR;
                }
                else
                {
                    if (frreport_body_l7_add_str(p_st_body_data, arr_name, p_value) == FR_FAIL)
                        goto ERROR;
                }
                frfree(p_len);
                p_len = NULL;
                frfree(p_value);
                p_value = NULL;
            }
            else
            {
                frfree(p_len);
                p_len = NULL;
                snprintf(arr_name, sizeof(arr_name), "response.redis.param.%d", ++i_count);
                if (frreport_body_l7_add_str(p_st_body_data, arr_name, "") == FR_FAIL)
                    goto ERROR;
            }
        }
        return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);
    }

    ui_len_tmp = bytebuff_length(p_packet)-2+1;
    p_value = (char*)frmalloc(ui_len_tmp);
    if (p_value == NULL)
        goto ERROR;
    memset(p_value, 0x00, ui_len_tmp);
    if (bytebuff_get_bytes(p_packet, p_value, ui_len_tmp-1) == FR_FAIL)
        goto ERROR;
    if (fris_print_ascii(p_value, ui_len_tmp-1)==0)
    {
        if (frreport_body_l7_add_bin(p_st_body_data, APM_REPORT_KEY_RESP_CODE_STRING, p_value, ui_len_tmp-1) == FR_FAIL)
            goto ERROR;
    }
    else
    {
        if (frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_RESP_CODE_STRING, p_value) == FR_FAIL)
            goto ERROR;
    }
    if (frreport_body_l7_add_int(p_st_body_data, APM_REPORT_KEY_RESP_CODE, 0) == FR_FAIL)
        goto ERROR;
    frfree(p_value);
    p_value = NULL;
    return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);

ERROR:
    if (p_value)
        frfree(p_value);
    if (p_len)
        frfree(p_len);
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * redis的packet解析
 *
 * \param p_st_conn          (入力链接信息)
 * \param p_st_packet       (入力packet信息)
 * \param p_st_hash_report   (保存Frreport_data)
 * \param p_st_session       (私有数据)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __req_data_analysis(Conntrack *p_st_conn
                                 , Apm_packet *p_st_packet
                                 , Frhash_header *p_st_hash_report
                                 , L7_session *p_st_session)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    L7session_redis_private *p_st_private = NULL;
    L4_transaction *p_st_l4_trans = NULL;
    Frreport_data *p_st_report_data = NULL;
    unsigned long ul_tmp = 0;

    if (p_st_conn == NULL || p_st_packet == NULL
        || p_st_hash_report == NULL || p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    p_st_private = (L7session_redis_private*)p_st_session->p_session_private;
    p_st_l4_trans = &(p_st_private->st_l4_trans);

    //如果连续的两个request
    if (p_st_private->p_lst_comm)
        __redis_session_reset((void *)(p_st_private));

    //创建数据链表
    if (p_st_private->p_st_body_data_req == NULL)
    {
        p_st_private->p_st_body_data_req = frreport_body_new_l7(p_st_conn);
        if (p_st_private->p_st_body_data_req == NULL)
            goto ERROR;

        if (frreport_body_l7_add_connInfo(p_st_private->p_st_body_data_req, p_st_conn) == FR_FAIL)
            goto ERROR;

        //seq ++
        p_st_private->ul_seq = frct_seq_increase(p_st_conn);

        //st_request_time_first_byte
        p_st_l4_trans->st_request_time_first_byte.tv_sec = p_st_packet->st_timestamp.tv_sec;
        p_st_l4_trans->st_request_time_first_byte.tv_usec = p_st_packet->st_timestamp.tv_usec;
    }

    /* 请求的字节数 */
    p_st_l4_trans->l_request_bytes += p_st_packet->ui_payload_len;

    //req解析
    i_ret = __req_parse(p_st_packet->p_payload, p_st_packet->ui_payload_len, p_st_private, p_st_private->p_st_body_data_req, p_st_conn);
    if (i_ret == APM_L7_ANALYZE_FAIL)
        goto ERROR;

    if (frreport_body_l7_add_str(p_st_private->p_st_body_data_req, APM_REPORT_KEY_SESSION_SERVER, "redis") == FR_FAIL)
        return FR_FAIL;

    if (frreport_body_l7_add_ulong(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_ID, p_st_private->ul_seq) == FR_FAIL)
        goto ERROR;

    //ResponseRequired
    if (frreport_body_l7_add_int(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_ISRESPREQUIRED, 1) == FR_FAIL)
        goto ERROR;

    //start time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_START_TIMESTAMP, &p_st_l4_trans->st_request_time_first_byte) == FR_FAIL)
        goto ERROR;

    //st_request_time
    p_st_l4_trans->st_request_time.tv_sec = p_st_packet->st_timestamp.tv_sec;
    p_st_l4_trans->st_request_time.tv_usec = p_st_packet->st_timestamp.tv_usec;

    ul_tmp = frtime_diff(&(p_st_l4_trans->st_request_time_first_byte), &(p_st_l4_trans->st_request_time));
    //send_time_us
    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_SEND_TIME_US, ul_tmp) == FR_FAIL)
        goto ERROR;

    //request bytes
    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_DATA_BYTES, p_st_l4_trans->l_request_bytes) == FR_FAIL)
        goto ERROR;

    //end time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_END_TIMESTAMP, &p_st_l4_trans->st_request_time) == FR_FAIL)
        goto ERROR;

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);
    if (p_st_report_data == NULL)
        goto ERROR;

    //拷贝相关链表
    p_st_private->p_lst_comm = FRREPORT_BODY_L7LIST_CLONE_COMM(p_st_private->p_st_body_data_req);
    if (p_st_private->p_lst_comm == NULL)
        goto ERROR;
    p_st_private->p_lst_req = FRREPORT_BODY_L7LIST_CLONE_REQ(p_st_private->p_st_body_data_req);
    if (p_st_private->p_lst_req == NULL)
        goto ERROR;

    if (frreport_data_add_body(p_st_report_data, p_st_private->p_st_body_data_req) == FR_FAIL)
        goto ERROR;
    p_st_private->p_st_body_data_req = NULL;

    if (frreport_data_hash(p_st_hash_report, p_st_report_data) == FR_FAIL)
        goto ERROR;
    p_st_report_data = NULL;

    return i_ret;

ERROR:
    if (p_st_report_data)
        frreport_data_free(&p_st_report_data);
    __redis_session_reset((void *)(p_st_private));
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * redis的packet解析
 *
 * \param p_st_conn          (入力链接信息)
 * \param p_st_packet       (入力packet信息)
 * \param p_st_hash_report   (保存Frreport_data)
 * \param p_st_session       (私有数据)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __resp_data_analysis(Conntrack *p_st_conn
                                 , Apm_packet *p_st_packet
                                 , Frhash_header *p_st_hash_report
                                 , L7_session *p_st_session)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    unsigned long ul_tmp = 0;
    L7session_redis_private *p_st_private = NULL;
    L4_transaction *p_st_l4_trans = NULL;
    Frreport_data* p_st_report_data = NULL;

    if (p_st_conn == NULL || p_st_packet == NULL
        || p_st_hash_report == NULL || p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    p_st_private = (L7session_redis_private*)p_st_session->p_session_private;
    p_st_l4_trans = &(p_st_private->st_l4_trans);

    if (p_st_private->p_st_body_data_resp == NULL)
    {
        //创建数据链表
        p_st_private->p_st_body_data_resp = frreport_body_new_l7(p_st_conn);
        if (p_st_private->p_st_body_data_resp == NULL)
            goto ERROR;

        if (p_st_l4_trans->st_request_time_first_byte.tv_sec != 0)
        {
            FRREPORT_BODY_L7LIST_SET_REQ(p_st_private->p_st_body_data_resp, &p_st_private->p_lst_req);
            FRREPORT_BODY_L7LIST_SET_COMM(p_st_private->p_st_body_data_resp, &p_st_private->p_lst_comm);
        }
        else
        {
            if (frreport_body_l7_add_connInfo(p_st_private->p_st_body_data_resp, p_st_conn) == FR_FAIL)
                goto ERROR;
        }

        p_st_l4_trans->st_response_time_first_byte.tv_sec = p_st_packet->st_timestamp.tv_sec;
        p_st_l4_trans->st_response_time_first_byte.tv_usec = p_st_packet->st_timestamp.tv_usec;

        //创建bytebuff
        if (p_st_private->p_resp_packet == NULL)
        {
            p_st_private->p_resp_packet = bytebuff_creat();
            if (p_st_private->p_resp_packet == NULL)
                goto ERROR;
        }
    }

    //请求的字节数
    p_st_l4_trans->l_response_bytes += p_st_packet->ui_payload_len;

    if (p_st_private->p_resp_packet == NULL)
        goto ERROR;
    //把这个包的数据先全加进bytebuff里
    if (bytebuff_add(p_st_private->p_resp_packet, p_st_packet->p_payload, p_st_packet->ui_payload_len) != FR_SUCC)
        goto ERROR;

    //resp解析
    i_ret = __resp_parse(p_st_private->p_resp_packet, p_st_private, p_st_private->p_st_body_data_resp, p_st_conn);
    if (i_ret == APM_L7_ANALYZE_FAIL)
        goto ERROR;
    if (i_ret == APM_L7_ANALYZE_TODO)
    {
        frreport_body_free(&p_st_private->p_st_body_data_resp);
        p_st_private->p_st_body_data_resp=NULL;
        bytebuff_rewind(p_st_private->p_resp_packet);
        return i_ret;
    }

    //数据上报
    //start time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_START_TIMESTAMP, &p_st_l4_trans->st_response_time_first_byte) == FR_FAIL)
        goto ERROR;

    if (p_st_l4_trans->st_request_time.tv_sec != 0 || p_st_l4_trans->st_request_time.tv_usec != 0)
    {
        //server time
        ul_tmp = frtime_diff(&(p_st_l4_trans->st_request_time), &(p_st_l4_trans->st_response_time_first_byte));
        if (frreport_body_l7_add_ulong(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_SERVER_TIME_US, ul_tmp) == FR_FAIL)
            goto ERROR;
    }
    else
    {
        if (frreport_body_l7_add_ulong(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_SERVER_TIME_US, 0) == FR_FAIL)
            goto ERROR;
    }

    //st_response_time
    p_st_l4_trans->st_response_time.tv_sec = p_st_packet->st_timestamp.tv_sec;
    p_st_l4_trans->st_response_time.tv_usec = p_st_packet->st_timestamp.tv_usec;

    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_DATA_BYTES, p_st_l4_trans->l_response_bytes) == FR_FAIL)
        goto ERROR;

    //down load time
    ul_tmp = frtime_diff(&(p_st_l4_trans->st_response_time_first_byte), &(p_st_l4_trans->st_response_time));
    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_DOWNLOAD_TIME_US, ul_tmp) == FR_FAIL)
        goto ERROR;

    //end time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_END_TIMESTAMP, &p_st_l4_trans->st_response_time) == FR_FAIL)
        goto ERROR;

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    if (p_st_report_data == NULL)
        goto ERROR;

    if (frreport_data_add_body(p_st_report_data, p_st_private->p_st_body_data_resp) == FR_FAIL)
        goto ERROR;
    p_st_private->p_st_body_data_resp = NULL;

    if (frreport_data_hash(p_st_hash_report, p_st_report_data) == FR_FAIL)
        goto ERROR;
    p_st_report_data = NULL;

    redis_session_destroy((void *)(p_st_private));
    p_st_session->p_session_private = NULL;

    return i_ret;

ERROR:
    if (p_st_report_data)
        frreport_data_free(&p_st_report_data);
    redis_session_destroy((void *)(p_st_private));
    p_st_session->p_session_private = NULL;
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * redis的packet解析
 *
 * \param p_st_conn          (入力链接信息)
 * \param p_st_packet        (入力packet信息)
 * \param p_st_hash_report   (保存Frreport_data)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
int redis_analysis(Conntrack *p_st_conn
                  , Apm_packet *p_st_packet
                  , Frhash_header *p_st_hash_report)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    L7_session *p_st_session = NULL;

    if (p_st_conn == NULL || p_st_packet == NULL || p_st_hash_report == NULL)
        return APM_L7_ANALYZE_FAIL;

    //到Conntrack中取得session信息
    p_st_session = frct_get_session(p_st_conn);
    if (p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    //fin syn包销毁私有数据
    if (p_st_packet->em_analyz_cmd == FRANALYZ_CMD_FIN || p_st_packet->em_analyz_cmd == FRANALYZ_CMD_SYN)
    {
        redis_session_destroy((void *)(p_st_session->p_session_private));
        p_st_session->p_session_private = NULL;
        return APM_L7_ANALYZE_SUCC;
    }

    //p_session_private分配内存
    if (p_st_session->p_session_private == NULL)
    {
        p_st_session->p_session_private = __redis_private_create();
        //内存分配失败
        if (p_st_session->p_session_private == NULL)
            return APM_L7_ANALYZE_FAIL;
    }

    //request
    if (p_st_packet->i_direction == FR_DIRECT_CLIENT2SRV)
    {
        i_ret = __req_data_analysis(p_st_conn,p_st_packet,p_st_hash_report,p_st_session);
        if (i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;

        return i_ret;
    }
    //response
    else
    {
        i_ret = __resp_data_analysis(p_st_conn,p_st_packet,p_st_hash_report, p_st_session);
        if (i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;

        return i_ret;
    }

ERROR:
    redis_session_destroy(p_st_session->p_session_private);
    p_st_session->p_session_private = NULL;
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * 私有信息销毁
 *
 * \param p_private_session  (redis的私有session数据)
 *
 * \return FR_SUCC   成功\n
 *
 * \note
 *
 */
int redis_session_destroy(void *p_private_session)
{
    L7session_redis_private *p_st_private = (L7session_redis_private*)p_private_session;

    if (p_st_private)
    {
        if (p_st_private->p_st_body_data_req)
            frreport_body_free(&p_st_private->p_st_body_data_req);

        if (p_st_private->p_st_body_data_resp)
            frreport_body_free(&p_st_private->p_st_body_data_resp);

        if (p_st_private->p_lst_req)
            frreport_body_l7list_free(&p_st_private->p_lst_req);

        if (p_st_private->p_lst_comm)
            frreport_body_l7list_free(&p_st_private->p_lst_comm);

        if (p_st_private->p_resp_packet)
            bytebuff_free(&(p_st_private->p_resp_packet));

        frfree(p_st_private);
    }
    return FR_SUCC;
}
